//
//  PywLoginAccountApi.m
//  PywSdk
//
//  Created by Jade on 16/8/1.
//  Copyright © 2016年 zero. All rights reserved.
//

#import "PywLoginAccountApi.h"
#import "AIFAccountLoginNoPwdManager.h"
#import "PywMBProgressHUD+Pyw.h"
#import "PywResourceUtils.h"
#import "PropertyListReformer.h"
#import "PywIAPHelper.h"
#import "PywSdk+login.h"
#import "PywCPNotifications.h"
@interface PywLoginAccountApi()<ETAPIManagerCallBackDelegate>
@end
@implementation PywLoginAccountApi
/**
 *  免密码登录
 */
- (void)requestLoginAccount:(NSString *)account
                    success:(Successd)success
                       fail:(Failure)fail
{
    self.successd = success;
    self.failure = fail;
    [PywMBProgressHUD showMessage:@"正在登录..."];
    
    AIFAccountLoginNoPwdManager *manager = [AIFAccountLoginNoPwdManager manager];
    manager.delegate = self;
    manager.account = account;
    [manager loadData];
}

#pragma mark - ETAPIManagerCallBackDelegate

/**
 *  请求失败
 *
 *  @param manager <#manager description#>
 */
- (void)managerCallApiDidFailed:(ETApiBaseManager *)manager
{
    [PywMBProgressHUD hideHUD];
    if (self.failure) {
        self.failure([self errorWithCode:0]);
    }
    
    [self sendLoginFailNotification];
}

/**
 *  请求成功
 *
 *  @param manager <#manager description#>
 */
- (void)managerCallAPIDidSuccess:(ETApiBaseManager *)manager
{
    [PywMBProgressHUD hideHUD];
    PropertyListReformer *reformer = [[PropertyListReformer alloc] init];
    NSDictionary *dict = [manager fetchDataWithReformer:reformer];
    if ([manager isKindOfClass:[AIFAccountLoginNoPwdManager class]]) {
        [self loginsuccess:dict];
    }
    
    
}
/**
 *  登录成功
 *
 *  @param dict <#dict description#>
 */
- (void)loginsuccess:(NSDictionary *)dict
{
    int resultCode = [[dict objectForKey:@"ack"] intValue];
    switch (resultCode) {
        case 200:
        {
            [self saveAccountLoginInfo:dict];
            if (self.successd) {
                self.successd(nil,nil);
            }
        }
            break;
        case 500:
        {
            if (self.failure) {
                self.failure([self errorWithCode:500]);
            }
        }
            break;
        default:
        {
            if (self.failure) {
                self.failure([self errorWithCode:0]);
            }
        }
            break;
    }
}

- (void)saveAccountLoginInfo:(NSDictionary *)dict
{
    [PywPlatformMain sharedPlatformMain].token = [NSString stringWithFormat:@"%@",dict[@"data"][@"token"]];
    [PywPlatformMain sharedPlatformMain].msisdn = [NSString stringWithFormat:@"%@",dict[@"data"][@"passport"]];
    [PywPlatformMain sharedPlatformMain].uid = [NSString stringWithFormat:@"%@",dict[@"data"][@"cp_uid"]];
    [PywPlatformMain sharedPlatformMain].account = [NSString stringWithFormat:@"%@",dict[@"data"][@"game_account"]];
    
    [[PywIAPHelper manager] checkLocalApplePay];
    
}
/**
 *  登录失败回调
 */
- (void)sendLoginFailNotification
{
    // 关闭时才发送登录完成的通知
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    // 发送登录完成的通知
    [dict setObject:[NSNumber numberWithInt:PYW_LOGIN_STATE_FAILURE] forKey:@"result"];
    [dict setObject:@"登录失败" forKey:@"info"];
    // 发送通知
    [[NSNotificationCenter defaultCenter] postNotificationName:kPywCPLoginNotification object:nil userInfo:dict];
}

@end
